package com.etc.web;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DownloadAttendanceExcel {

    @Test
    @RequestMapping("/DownloadAttendanceExcel")
    public void export(HttpServletResponse response, String AttendExcelname, String AttendExceltitle, String AttendExcelsheetName, HttpSession httpSession) throws Exception{
        String Excelsuffex = ".xls";
        String Excelname= AttendExcelname+Excelsuffex;
        TemplateExportParams params = new TemplateExportParams("static/excel/attendance.xlsx");
        List<Map<String, String>> maps = (List<Map<String, String>>)httpSession.getAttribute("exportExcelList");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("maplist",maps);
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        File savefile = new File("C:/excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("C:/excel/"+Excelname);
        workbook.write(fos);
        fos.close();
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(Excelname, "UTF-8"));
        workbook.write(response.getOutputStream());
       //return "redirect:/Teacher/login/TeacherExam/loginUser/attendance?classnode="+httpSession.getAttribute("classnode");
    }





}
